<?php
/**
 * 预约管理
 * @author a
 *
 */
class Book extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    private $substr_num = 10;   //列表显示留言数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
        
        $this->load->model('book_model');  
        $this->load->model('book_member_model');  
        $this->load->model('course_cate_model');  
        $this->load->model('course_list_model');  
        $this->load->model('course_package_model');
        $this->load->model('teacher_model');
        
        //分页
        $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
        $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
        $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    /**
     * 订单列表
     */
    public function Index(){
        try {
            //$fild = 'id as order_id,course_package_id,hours,surplus_hours,status';
            $where = "nursery_id = '$this->_nursery_id'";
            $book_list = $this->book_model->findAll($where,'*','id desc',$this->offse,$this->page_size);
            if(empty($book_list))throw new Exception('没有约课记录');
            
            $total = $this->book_model->findCount($where); //总数
            $pagelink = $this->pagination($total,$this->page_size);
            
            //获取教师
            $where = "id IN (". filterValue($book_list,'teacher_id',true) .")";
            $teacher_list = $this->teacher_model->findAll($where,'id,name');
            $teacher_list = restByKey($teacher_list,'id');
            
            //获取用户名
            $where = "id IN (". filterValue($book_list,'book_member_id',true) .")";
            $member_list = $this->book_member_model->findAll($where,'id,baby_name,linkman');
            $member_list = restByKey($member_list,'id');
                        
            //获取课包信息
            $where = "id IN (". filterValue($book_list,'course_package_id',true) .")";
            $course_package_list = $this->course_package_model->findAll($where,'id,title as cp_title,course_list_id');
            $course_package_list = restByKey($course_package_list,'id');
                        
            //获取课程列表
            $where = "id IN (". filterValue($course_package_list,'course_list_id',true) .")";
            $course_list = $this->course_list_model->findAll($where,'id,title as c_title,course_cate_id');
            $course_list = restByKey($course_list,'id');
            
            //获取课程分类
            $where = "id IN (". filterValue($course_list,'course_cate_id',true) .")";
            $course_cate_list = $this->course_cate_model->findAll($where,'id,title as cc_title');
            $course_cate_list = restByKey($course_cate_list,'id');

            //合并
            $course_list = arrayMerge($course_list,'course_cate_id',$course_cate_list);
            $course_package_list = arrayMerge($course_package_list,'course_list_id',$course_list);
            $book_list = arrayMerge($book_list,'course_package_id',$course_package_list);
            
            //echo '<pre>';print_r($book_list);die();
            $this->t->write_view('content', 'nursery/book/index', array(
                'pagelink'     =>  $pagelink,
                'book_list'    =>  $book_list,
                'member_list'  =>  $member_list,
                'teacher_list' =>  $teacher_list,
                'book_status'  => array('待上课','上课中','已下课','已结束','取消预约'),
                'substr_num'   =>   $this->substr_num,
            ), false);
            
            $this->t->render();
            
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'nursery/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/nursery/book/',
            ), false);
            $this->t->render();
        }
        
    }
    /**
     * 详情
     * @throws Exception
     */
    public function info(){
        try {
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('约课ID为空');
            $where['nursery_id'] = $this->_nursery_id;
            $where['id'] = $id;
            $book_list = $this->book_model->find($where);
            if(empty($book_list))throw new Exception('没有约课记录');
        
            //获取用户名
            $where = "id = '{$book_list['book_member_id']}'";
            $member_list = $this->book_member_model->find($where);
            
            //获取教师
            $where = "id = '{$book_list['teacher_id']}'";
            $teacher_list = $this->teacher_model->find($where);
        
            //获取课包信息
            $where = "id = '{$book_list['course_package_id']}'";
            $course_package_list = $this->course_package_model->find($where,'id,title,course_list_id');
            
            //获取课程列表
            $where = "id = '{$course_package_list['course_list_id']}'";
            $course_list = $this->course_list_model->find($where,'id,title,course_cate_id');
            
            //获取课程分类
            $where = "id = '{$course_list['course_cate_id']}'";
            $course_cate_list = $this->course_cate_model->find($where,'id,title'); 
        
            //echo '<pre>';print_r($order_list);die();
            $this->t->write_view('content', 'nursery/book/info', array(
                'teacher_list' =>  $teacher_list,
                'book_list'    =>  $book_list,
                'member_list'  =>  $member_list,
                'course_cate_list' =>  $course_cate_list,
                'course_list'  =>  $course_list,
                'course_package_list'  =>  $course_package_list,
                'order_status'  => array('待上课','上课中','已下课','已结束','取消预约'),
            ), false);
            $this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'nursery/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/nursery/book/',
            ), false);
            $this->t->render();
        }
    }   
    /**
     * 修改状态
     */ 
    public function Updatestatus(){
        try {
            $result = array('code'=>10,'msg'=>'修改成功');
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('约课ID为空');
            $status = $this->input->get('status');
            if($status == null || !is_numeric($status) || $status > 4)throw new Exception('状态类型错误');
            
            $bool = $this->book_model->update(array('status'=>$status),array('id'=>$id));
            if(empty($bool))throw new Exception('修改失败');
            
        }catch (Exception $e){
            $result['code'] = 11;
            $result['msg'] = $e->getMessage();
        }
        echo json_encode($result);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}






























